package edu.kjxy.day01.controller;

/**
 * 功能:使用synchronized关键字来解决”非线程安全"
 * 作者:程序员欧阳子韩
 * 日期:2024/1/31 18:55
 */
public class LoginServlet {
    private static String usernameRef;
    private static String passwordRef;
    //线程安全
    synchronized public static void doPost(String username , String password){
        try {
            usernameRef = username;
            if (username.equals("a")){
                Thread.sleep(5000);
            }
            passwordRef = password;
            System.out.println("username=" + usernameRef + "password=" + password);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
//    public static void doPost(String username , String password){
//        try {
//            usernameRef = username;
//            if (username.equals("a")){
//                Thread.sleep(5000);
//            }
//            passwordRef = password;
//            System.out.println("username=" + usernameRef + "password=" + password);
//        }catch (InterruptedException e){
//            e.printStackTrace();
//        }
//    } 非线程安全
}
